package com.keba.lamp.complaint.event.listener;

import com.keba.lamp.complaint.biz.AccountBiz;
import com.keba.lamp.system.event.TenantEvent;
import com.keba.lamp.system.event.TenantEventVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;

@Component
@Slf4j
@RequiredArgsConstructor
public class TenantEventListener {

    private final AccountBiz accountBiz;

    @Async
    @TransactionalEventListener({TenantEvent.class})
    public void handleAccount(TenantEvent event) {
        TenantEventVO tenantEventVO = (TenantEventVO) event.getSource();
        tenantEventVO.write();
        if (tenantEventVO.isUpdate()) {
            accountBiz.updateAccount(tenantEventVO.getTenantId(), tenantEventVO.getTenantName());
            return;
        }
        accountBiz.addAccount(tenantEventVO.getTenantId(), tenantEventVO.getTenantName());
    }
}
